﻿using System;
using System.Collections.Generic;
using System.Xml;

namespace JUtil.CodeGenerator
{
    class ProjectConfig : IBuildXmlElement
    {
        public ProjectConfig() { }

        public ProjectConfig(string ProjectName)
        {
            this.ProjectName = ProjectName;
            ExploreAfterRun = false;
            NAMESPACE = ProjectName;
            ConnectionString = string.Empty;
            DbProvider = default(DbProvider);

            DatabaseType = default(DatabaseType);
            Tables = null;
            ExportDirectory = string.Empty;
            ExportMode = default(ProjectDataGG.ExportMode);
        }

        public void Update(ProjectConfig another)
        {
            ProjectName = another.ProjectName;
            ExploreAfterRun = another.ExploreAfterRun;
            NAMESPACE = another.NAMESPACE;
            ConnectionString = another.ConnectionString;
            DbProvider = another.DbProvider;
            DatabaseType = another.DatabaseType;

            Tables = another.Tables;
            ExportDirectory = another.ExportDirectory;
            ExportMode = another.ExportMode;
        }

        public ProjectConfig(XmlElement el)
        {
            ProjectName = Convert.ToString(el.Attributes["ProjectName"].Value);
            ExploreAfterRun = Convert.ToString(el.Attributes["ExploreAfterRun"].Value).Equals("1") ? true : false;
            NAMESPACE = Convert.ToString(el.Attributes["NAMESPACE"].Value);
            ConnectionString = Convert.ToString(el.Attributes["ConnectionString"].Value);
            DbProvider = (DbProvider)Convert.ToInt32(el.Attributes["DbProvider"].Value);
            DatabaseType = (DatabaseType)Convert.ToInt32(el.Attributes["DatabaseType"].Value);

            Tables = Convert.ToString(el.Attributes["Tables"].Value).ToList();
            ExportDirectory = Convert.ToString(el.Attributes["ExportDirectory"].Value);
            ExportMode = (ProjectDataGG.ExportMode)Convert.ToInt32(el.Attributes["ExportMode"].Value);
        }

        public XmlElement BuildXmlElement(XmlDocument doc)
        {
            XmlElement el = doc.CreateElement("ProjectConfig");

            el.SetAttribute("ProjectName", ProjectName);
            el.SetAttribute("ExploreAfterRun", ExploreAfterRun ? "1" : "0");
            el.SetAttribute("NAMESPACE", NAMESPACE);
            el.SetAttribute("ConnectionString", ConnectionString);
            el.SetAttribute("DbProvider", ((int)DbProvider).ToString());

            el.SetAttribute("DatabaseType", ((int)DatabaseType).ToString());
            el.SetAttribute("Tables", Tables.Combinate());
            el.SetAttribute("ExportDirectory", ExportDirectory);
            el.SetAttribute("ExportMode", ((int)ExportMode).ToString());

            return el;
        }

        /// <summary>
        /// 專案名稱
        /// </summary>
        public string ProjectName { get; set; }

        /// <summary>
        /// 產製完成後開啟匯出資料夾
        /// </summary>
        public bool ExploreAfterRun { get; set; }

        /// <summary>
        /// 專案命名空間
        /// </summary>
        public string NAMESPACE { get; set; }

        /// <summary>
        /// 專案連線字串
        /// </summary>
        public string ConnectionString { get; set; }

        /// <summary>
        /// 專案用來連線資料庫的Provider
        /// </summary>
        public DbProvider DbProvider { get; set; }

        /// <summary>
        /// 專案連線資料庫的類型
        /// </summary>
        public DatabaseType DatabaseType { get; set; }

        /// <summary>
        /// 要產生TableDataGenerator的資料表
        /// </summary>
        public List<string> Tables { get; set; }

        /// <summary>
        /// 匯出資料夾路徑
        /// </summary>
        public string ExportDirectory { get; set; }

        /// <summary>
        /// 匯出檔案類型
        /// </summary>
        public ProjectDataGG.ExportMode ExportMode { get; set; }

        public ProjectDataGGConfig ToProjectDataGGConfig()
        {
            return new ProjectDataGGConfig
            {
                NAMESPACE = NAMESPACE,
                ConnectionString = ConnectionString,
                DbProvider = DbProvider,
                DatabaseType = DatabaseType,
                Tables = Tables,

                ExportDirectory = ExportDirectory,
                ExportMode = ExportMode,
            };
        }
    }
}
